package cool.zhoujie.address;

import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.yanzhenjie.recyclerview.OnItemMenuClickListener;
import com.yanzhenjie.recyclerview.SwipeMenu;
import com.yanzhenjie.recyclerview.SwipeMenuBridge;
import com.yanzhenjie.recyclerview.SwipeMenuCreator;
import com.yanzhenjie.recyclerview.SwipeMenuItem;
import com.yanzhenjie.recyclerview.SwipeRecyclerView;
import java.util.List;
import cool.zhoujie.address.adapter.ShowItemAdapter;
import cool.zhoujie.address.databinding.FragmentShowBinding;
import cool.zhoujie.address.db.Address;
import cool.zhoujie.address.viewmodel.ShowFragmentViewModel;

public class ShowFragment extends Fragment {

    public ShowFragment() {
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        FragmentShowBinding binding = DataBindingUtil.inflate(inflater, R.layout.fragment_show, container, false);
        final ShowFragmentViewModel viewmodel = new ViewModelProvider(this).get(ShowFragmentViewModel.class);
        binding.setViewmodel(viewmodel);
        final SwipeRecyclerView recyclerView = binding.getRoot().findViewById(R.id.show_list);

        LinearLayoutManager manager = new LinearLayoutManager(this.getContext());
        recyclerView.setLayoutManager(manager);
        viewmodel.mList.observe(getViewLifecycleOwner(), new Observer<List<Address>>() {
            @Override
            public void onChanged(List<Address> list) {
                ShowItemAdapter adapter = new ShowItemAdapter(list,viewmodel);
                recyclerView.setAdapter(adapter);
            }
        });

        //实现滑动删除
        initItemSwipe(recyclerView,viewmodel);

        binding.setLifecycleOwner(this);
        return binding.getRoot();
    }

    private void initItemSwipe(SwipeRecyclerView recyclerView, final ShowFragmentViewModel viewmodel) {
        SwipeMenuCreator swipeMenuCreator = new SwipeMenuCreator() {
            @Override
            public void onCreateMenu(SwipeMenu leftMenu, SwipeMenu rightMenu, int position) {
                SwipeMenuItem defaultItem = new SwipeMenuItem(getContext());
                defaultItem.setText("设为默认")
                        .setTextColor(getResources().getColor(R.color.black))
                        .setTextSize(14)
                        .setHeight(ViewGroup.LayoutParams.MATCH_PARENT)
                        .setWidth(240);
                rightMenu.addMenuItem(defaultItem);

                SwipeMenuItem deleteItem = new SwipeMenuItem(getContext());
                deleteItem.setWidth(180)
                        .setHeight(ViewGroup.LayoutParams.MATCH_PARENT)
                        .setText("删除")
                        .setTextSize(14)
                        .setTextColor(getResources().getColor(R.color.white))
                        .setBackgroundColor(getResources().getColor(R.color.red));
                rightMenu.addMenuItem(deleteItem);
            }
        };
        //设置滑动监听
        recyclerView.setSwipeMenuCreator(swipeMenuCreator);

        OnItemMenuClickListener itemMenu = new OnItemMenuClickListener() {
            @Override
            public void onItemClick(SwipeMenuBridge menuBridge, final int adapterPosition) {
                menuBridge.closeMenu();

                final int menuPosition = menuBridge.getPosition();

                Address address = viewmodel.list.get(adapterPosition);
                if (menuPosition == 0) {
                    //设为默认
                    address.isDefault = true;
                    viewmodel.mDao.updateDefault(false);
                    viewmodel.mDao.update(address);
                    //刷新
                    viewmodel.mList.setValue(viewmodel.mDao.queryAll());
                } else {
                    //删除
                    viewmodel.mDao.delete(address);
                    //刷新
                    viewmodel.mList.setValue(viewmodel.mDao.queryAll());
                }

            }
        };

        //设置菜单点击监听
        recyclerView.setOnItemMenuClickListener(itemMenu);
    }

}
